﻿using System.Collections.Generic;
using System.Linq;

namespace YourLastOptionsDialog.ViewModels.Options
{
    /// <summary>
    /// An option that represents an enumeration value.
    /// </summary>
    public class EnumOption : Option
    {
        private IList<EnumValue> _lookup;

        /// <summary>
        /// Gets or sets the value wrapped by this option.
        /// </summary>
        public EnumValue Value
        {
            get
            {
                var originalValue = GetAccessor().ToString();
                var result = Lookup.Where(o => o.OriginalValue.ToString() == originalValue).First();
                return result;
            }

            set
            {
                // get original value
                var originalValue = value != null ? value.OriginalValue : null;
                SetAccessor(originalValue);
                RaisePropertyChanged("Value");
            }
        }

        /// <summary>
        /// Gets the look up collection of all possible values for the underlying enumeration type.
        /// </summary>
        public IList<EnumValue> Lookup
        {
            get
            {
                return _lookup;
            }
        }

        /// <summary>
        /// Sets the lookup values.
        /// </summary>
        /// <param name="lookupValues">The lookup values to use.</param>
        public void SetLookupValues(IEnumerable<EnumValue> lookupValues)
        {
            if (_lookup == null)
            {
                _lookup = new List<EnumValue>(lookupValues);
            }
            else
            {
                Lookup.Clear();

                if (lookupValues != null)
                {
                    foreach (var lookupValue in lookupValues)
                    {
                        Lookup.Add(lookupValue);
                    }
                }
            }
        }
    }
}